<template>
  <div>
    <el-upload
      multiple
      name="myfile"
      ref="onceUpload"
      class="upload-demo"
      :data="uplodaData"
      :headers="headers"
      :on-preview="handlePreview"
      :action="uplodaUrl"
      :on-remove="handleRemove"
      :file-list="fileUploads"
      :http-request="httpRequest"
      :auto-upload="false"
      :on-change="handleBefore"
      :before-upload="handleBefore"
      accept=".jpg, .jpeg, .png, .gif, .bmp, .bpm, .JPG, .JPEG, .PBG, .GIF, .BMP, .BPM"
    >
      <el-button size="small" type="primary" icon="el-icon-upload">上传文件</el-button>
    </el-upload>
  </div>
</template>

<script> 
import { getToken } from "@/utils/auth";
export default {
  name: "commonOnceUpload",
  props: {
    uplodaData: {
      type: Object,
      default: {}
    },
    taskMatterId: {},
    firstStepFalg: {
      type: Boolean,
      default: true
    },
    fileUploads: {
      type: Array,
      default: function() {
        return [];
      }
    },
    form: {
      type: Object,
      default: {}
    }
  },
  watch: {
    taskMatterId: function() {
      this.setTaskMatterId();
    }
  },

  created() {
    this.setTaskMatterId();
  },
  data() {
    return {
      headers: { Authorization: "Bearer " + getToken() },
      uplodaUrl: _cfg.serverUrl + "/task/upload/add",
      file: [] //多个文件的集合
    };
  },
  methods: {
    //自定义的请求方式
    httpRequest(param) {
      this.file.push(param.file); // 一般情况下是在这里创建FormData对象，但我们需要上传多个文件，为避免发送多次请求，因此在这里只进行文件的获取，param可以拿到文件上传的所有信息
    },
    handleRemove(val) {
      if (typeof val.id != "undefined") { 
      }
    },
    //设置任务ID
    async setTaskMatterId() {
      console.log(isNaN(this.taskMatterId), "this.taskMatterId");
      if (this.taskMatterId) {
        this.uplodaData.processid = this.taskMatterId;
      }
    },
    //处理下载
    handlePreview(val) {
      this.downloadFile(val.url);
    },
    //上传前相关判断
    handleBefore(file) {
      var testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      const extension =
        testmsg === "jpg" ||
        testmsg === "JPG" ||
        testmsg === "png" ||
        testmsg === "PNG" ||
        testmsg === "bpm" ||
        testmsg === "BPM" ||
        testmsg === "bmp" ||
        testmsg === "BMP";
      const isLt50M = file.size / 1024 / 1024 < 10;
      if (!extension) {
        this.$message({
          message: "上传图片只能是jpg / png / bpm / bmp格式!",
          type: "error"
        });
        return false; //必须加上return false; 才能阻止
      }
    },
    // /** 图片上传同步 */
    uploadFile(val) {
      //上传图片
      var vm = this;
      var upData = new FormData();
      this.$refs.onceUpload.submit();
      this.file.forEach(function(file) {
        upData.append("myfile", file, file.name);
      });
      upData.append("processid", val.data.id);
      Object.keys(vm.form).forEach(function(key) {
        upData.append(key, vm.form[key]);
        console.log(key, vm.form[key]);
      }); 
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
